$(function () {
    //定义layui里的方法
    let form = layui.form
    let layer = layui.layer
    //渲染数据的函数
    initArtCateList()
    function initArtCateList() {
        axios({
            url: '/my/article/cates',
            method: 'get',
        }).then(res => {
            //成功回调
            //定义一个数组
            let htmlArr = []
            //遍历数据放入数组
            res.data.data.forEach(value => {
                //放入数组
                htmlArr.push(`<tr>
                <td>${value.Id}</td>
                <td>${value.name}</td>
                <td>${value.alias}</td>
                <td>
                    <button type="button" data-id=${value.Id} class="btn-edit layui-btn layui-btn-xs">修改</button>
                    <button type="button" data-id=${value.Id} class="del-edit layui-btn layui-btn-xs layui-btn-danger">删除</button>
                </td>
            </tr>`)
            })
            //渲染数据empty()删除里面原有的数据
            $('tbody').empty().html(htmlArr)
        })
    }
    //用来关闭询问框
    let indexAdd = 0
    //添加图书的输入框
    $('#addBtn').on('click', function () {
        //弹出询问框，内容里面添加表单
        indexAdd = layer.open({
            type: 1,
            title: '添加文章分类',
            area: ['500px', '260px'],
            content: `
                <form id="addForm" class="layui-form" action="">
                        <!-- 1.分类 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 2.别名 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类别名</label>
                            <div class="layui-input-block">
                                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 3.按钮 -->
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                </form>
        `,
        })

    })
    //添加图书
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault()
        axios({
            url: '/my/article/addcates',
            method: 'post',
            data: $(this).serialize() //获取form表单所有的数据
        }).then(res => {
            if (res.data.status != 0) return layer.msg(res.data.message)
            layer.msg(res.data.message)
            //渲染数据
            initArtCateList()
            //关闭页面
            layer.close(indexAdd)
        })
    })
    let editAdd = 0
    //修改的弹出框，根据id修改数据。自定义属性获取点击元素的id
    $('body').on('click', '.btn-edit', function () {
        //弹出框
        editAdd = layer.open({
            type: 1,
            title: '添加文章分类',
            area: ['500px', '260px'],
            content: `
                <form id="editForm" class="layui-form" action="" lay-filter="editForm">
                        <!-- 0.分类 -->
                        <!-- 用于存放id的数据 -->
                        <input type="hidden" name="Id">
                        <!-- 1.分类 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 2.别名 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类别名</label>
                            <div class="layui-input-block">
                                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 3.按钮 -->
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即修改</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                </form>
        `
        })
        //获取自定义元素的id属性，给window添加可以成为全局的，还可以省略window
        let id = $(this).attr('data-id')
        //渲染数据
        axios({
            //id直接在请求体中添加
            url: '/my/article/cates/' + id,
            method: 'get',

        }).then(res => {
            //成功回调
            if(res.data.status != 0) return layer.msg(res.data.message)
            //通过lay-filter 属性找到需要添加数据的form表单，传值就是添加，不传值就是查询
            layui.form.val('editForm',res.data.data)//id也添加进去了
        })
    })
    //发送ajax,修改数据
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()
        axios({
            url:'/my/article/updatecate',
            method:'post',
            data:  $(this).serialize()//自动收集表单的name属性
            
        }).then(res=>{
            //成功回调
            if(res.data.status != 0) return layer.msg(res.data.message)
            layer.msg(res.data.message)
            //渲染数据
            initArtCateList()
            //关闭弹出框
            layer.close(editAdd)
        })

    })
    //删除数据
    $('body').on('click','.del-edit',function(){
        let id = $(this).attr('data-id')
        layer.confirm('是否确认删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            axios({
                url: '/my/article/deletecate/' + id,
                method: 'get',

            }).then(res => {
                if (res.data.status != 0) {
                    return layer.msg(res.data.message)
                }
                //成功提示
                layer.msg(res.data.message)
            })
            //关闭弹出框
            layer.close(index);
            //渲染数据
            initArtCateList()
        })
    })


})